<?php
/*
 * File:format.class.php
 * Created:2013-5-17
 * Author:oldjiang
 * 
 */
class format {

	/**
	 * 移除Html代码和单引号"'"
	 */
    function removeHtml($content){
		//$content=preg_replace("/<a[^>]*>/i",'',$content);
		//$content=preg_replace("/<\/a>/i",'',$content);
		$content=preg_replace("/&([a-zA-Z])*;/i",'',$content);
		$content=preg_replace("/'/i",'',$content);
		return strip_tags(addslashes($content));
    }
    
    /**
     * 移除DZ代码
     */
    function removeDZCode($message){
		$message = str_replace(array(
			'[/color]', '[/backcolor]', '[/size]', '[/font]', '[/align]', '[b]', '[/b]', '[s]', '[/s]', '[hr]', '[/p]',
			'[i=s]', '[i]', '[/i]', '[u]', '[/u]', '[list]', '[list=1]', '[list=a]',
			'[list=A]', "\r\n[*]", '[*]', '[/list]', '[indent]', '[/indent]', '[/float]'
			), array(
			'</font>', '</font>', '</font>', '</font>', '</p>', '<strong>', '</strong>', '<strike>', '</strike>', '<hr class="l" />', '</p>', '<i class="pstatus">', '<i>',
			'</i>', '<u>', '</u>', '<ul>', '<ul type="1" class="litype_1">', '<ul type="a" class="litype_2">',
			'<ul type="A" class="litype_3">', '<li>', '<li>', '</ul>', '<blockquote>', '</blockquote>', '</span>'
			), preg_replace(array(
			"/\[color=([#\w]+?)\]/i",
			"/\[color=(rgb\([\d\s,]+?\))\]/i",
			"/\[backcolor=([#\w]+?)\]/i",
			"/\[backcolor=(rgb\([\d\s,]+?\))\]/i",
			"/\[size=(\d{1,2}?)\]/i",
			"/\[size=(\d{1,2}(\.\d{1,2}+)?(px|pt)+?)\]/i",
			"/\[font=([^\[\<]+?)\]/i",
			"/\[align=(left|center|right)\]/i",
			"/\[p=(\d{1,2}|null), (\d{1,2}|null), (left|center|right)\]/i",
			"/\[float=left\]/i",
			"/\[float=right\]/i"

			), array(
			"<font color=\"\\1\">",
			"<font style=\"color:\\1\">",
			"<font style=\"background-color:\\1\">",
			"<font style=\"background-color:\\1\">",
			"<font size=\"\\1\">",
			"<font style=\"font-size:\\1\">",
			"<font face=\"\\1\">",
			"<p align=\"\\1\">",
			"<p style=\"line-height:\\1px;text-indent:\\2em;text-align:\\3\">",
			"<span style=\"float:left;margin-right:5px\">",
			"<span style=\"float:right;margin-left:5px\">"
			), $message));
		$message = preg_replace("/attach:\/\/(\d+)\.?(\w*)/ie", "", $message);
		$message = preg_replace("/ed2k:\/\/(.+?)\//e", "", $message);
		$message = preg_replace("/\[email(=([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+))?\](.+?)\[\/email\]/ies", "", $message);
		$message = preg_replace("/\[url(=((https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|thunder|qqdl|synacast){1}:\/\/|www\.|mailto:)?([^\r\n\[\"']+?))?\](.+?)\[\/url\]/ies", "", $message);
		$message = preg_replace("/\s?\[quote\][\n\r]*(.+?)[\n\r]*\[\/quote\]\s?/is", "", $message);
		$message = preg_replace("/\s*\[free\][\n\r]*(.+?)[\n\r]*\[\/free\]\s*/is", "", $message);
		$message = preg_replace("/\[media=([\w,]+)\]\s*([^\[\<\r\n]+?)\s*\[\/media\]/ies", "", $message);
		$message = preg_replace("/\[audio(=1)*\]\s*([^\[\<\r\n]+?)\s*\[\/audio\]/ies", "", $message);
		$message = preg_replace("/\[flash(=(\d+),(\d+))?\]\s*([^\[\<\r\n]+?)\s*\[\/flash\]/ies", "", $message);
		$message = preg_replace("/\[hide\]\s*(.*?)\s*\[\/hide\]/is", "", $message);
		$message = preg_replace("/\[hide\](.*?)\[\/hide\]/is", "", $message);
		$message = preg_replace("/\[hide=(\d+)\]\s*(.*?)\s*\[\/hide\]/ies", "", $message);
		$message = preg_replace("/\[swf\]\s*([^\[\<\r\n]+?)\s*\[\/swf\]/ies", "", $message);
		$message = preg_replace(array(
				"/\[img\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/ies",
				"/\[img=(\d{1,4})[x|\,](\d{1,4})\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/ies"
			),array(
				"",
				""
			), $message);
		//清除代码内容
		$message = str_replace("[\tDISCUZ_CODE_*\t]", "", $message);
		//清除DZ表情
		$message=preg_replace("/{:([0-9])*_([0-9])*:}/i",'',$message);
		//清除表格标签
		$message=preg_replace("/\[table(=)?([0-9])*(%)?(,)?(#)?([a-zA-Z0-9])*\]/i",'',$message);
		$message=preg_replace("/\[td(=)?([0-9,])*(%)?\]/i",'',$message);
		$message=preg_replace("/\[tr(=)?([0-9])*(%)?\]/i",'',$message);
		$message=preg_replace("/\[\/td\]/i",'',$message);
		$message=preg_replace("/\[\/tr\]/i",'',$message);
		$message=preg_replace("/\[\/table\]/i",'',$message);
		//清除附件标签
		$message=preg_replace("/\[attach\]([0-9])*\[\/attach\]/i",'',$message);
		$message = $this -> removeHtml($message);
    	return strip_tags(addslashes($message));
    }
  
  	/**
  	 * 标记关键词
  	 */  
    function mark($keyword,$content){
    	return str_ireplace($keyword,'<b style="color:red;">'.$keyword.'</b>',$content);
    }
    
    /**
     * 截取字符串
     */
	 function utf_substr($str,$len){
		for($i=0;$i<$len;$i++){
			$temp_str=substr($str,0,1);
			if(ord($temp_str) > 127){
				$i++;
				if($i<$len){
					$new_str[]=substr($str,0,3);
					$str=substr($str,3);
				}
			}else{
				$new_str[]=substr($str,0,1);
				$str=substr($str,1);
			}
		}
		return join($new_str);
	}
	
	/**
	 * 判断是否是数字，是数字则返回，不是数字则返回为0
	 */
	function isNumeric($num){
		if(is_numeric($num)){
			return $num;
		}else{
			return 0;
		}
	}

}
?>